package com.farm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.farm.common.R;
import com.farm.common.esIndex;
import com.farm.mapper.CommitMapper;
import com.farm.mapper.PolicyMapper;
import com.farm.mapper.VideoMapper;
import com.farm.pojo.*;

import com.farm.pojo.Vo.PolicyEsVo;
import com.farm.service.EsCommitService;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.*;

@Service
public class EsCommitServiceImpl implements EsCommitService {
    @Autowired
    private PolicyMapper policyMapper;

    @Autowired
    private CommitMapper commitMapper;

    @Autowired
    private VideoMapper videoMapper;


    /**
     * 解析自动补全数据
     * @param maps
     * @param column
     * @return
     */
    public List<Map<String, String>> handlePrefix(List<Map<String, Object>> maps, String column){
        List<Map<String, String>> list = new ArrayList<>();
        int length = maps.size();
        if ( length > 0 && maps != null) {
            for (int i = 0; i < length; i++){
                String label = (String) maps.get(i).get(column);
                Map<String, String> textMap = new HashMap<>();
                textMap.put("value", label);
                list.add(textMap);
            }
        }
        return list;
    }

    /**
     * 自动补全功能
     *
     * @param prefix
     * @return
     */
    @Override
    public List<Map<String, String>> getSuggestion(String prefix, String path) {
        List<Map<String, String>> list = new ArrayList<>();

        // 资源表
        switch (path) {
            case esIndex.policy:
                QueryWrapper<Policy> wrapper = new QueryWrapper<>();
                wrapper.like("label", prefix).select("label");
                List<Map<String, Object>> maps = policyMapper.selectMaps(wrapper);
                list = handlePrefix(maps, "label");
                return list;

            case esIndex.commit:
                QueryWrapper<Commited> commitWrapper = new QueryWrapper<>();
                commitWrapper.like("label", prefix).select("label");
                List<Map<String, Object>> commitMaps = commitMapper.selectMaps(commitWrapper);
                list = handlePrefix(commitMaps, "label");
                return list;

            case esIndex.video:
                QueryWrapper<Video> videoWrapper = new QueryWrapper<>();
                videoWrapper.like("name", prefix).select("name");
                List<Map<String, Object>> videoMaps = videoMapper.selectMaps(videoWrapper);
                list = handlePrefix(videoMaps, "name");
                return list;

            default:
                break;
        }

        System.out.println(list.toString());
        return list;
    }


    /**
     * 解析policy表
     *
     * @param policyList
     * @return
     */
    public List<PolicyEsVo> handlePolicy(List<Policy> policyList) {
        List<PolicyEsVo> policyEsVoList = new ArrayList<>();
        int length = policyList.size();
        if (policyList != null && length > 0) {
            int sort = 0;
            for (int i = 0; i < length; i++) {
                Policy policy = policyList.get(i);
                sort++;
                PolicyEsVo policyEsVo = new PolicyEsVo();
                BeanUtils.copyProperties(policy, policyEsVo);
                policyEsVo.setSort(sort);
                policyEsVoList.add(policyEsVo);
            }
        }
        return policyEsVoList;
    }


    /**
     * 解析commit表
     *
     * @param commitedList
     * @return
     */
    public List<PolicyEsVo> handleCommit(List<Commited> commitedList) {
        List<PolicyEsVo> policyEsVoList = new ArrayList<>();
        int length = commitedList.size();
        if (commitedList != null && length > 0) {
            int sort = 0;
            for (int i = 0; i < length; i++) {
                Commited commited = commitedList.get(i);
                sort++;
                PolicyEsVo policyEsVo = PolicyEsVo.builder()
                        .sort(sort)
                        .content(commited.getContent())
                        .title(commited.getLabel())
                        .build();
                policyEsVoList.add(policyEsVo);
            }
        }
        return policyEsVoList;
    }

    /**
     * 解析video表
     *
     * @param videoList
     * @return
     */
    public List<PolicyEsVo> handleVideo(List<Video> videoList) {
        List<PolicyEsVo> policyEsVoList = new ArrayList<>();
        int length = videoList.size();
        if (videoList != null && length > 0) {
            int sort = 0;
            for (int i = 0; i < length; i++) {
                Video video = videoList.get(i);
                sort++;
                PolicyEsVo policyEsVo = PolicyEsVo.builder()
                        .sort(sort)
                        .content(video.getPath())
                        .title(video.getName())
                        .build();
                policyEsVoList.add(policyEsVo);
            }
        }
        return policyEsVoList;
    }

    /**
     * 搜索数据
     *
     * @param esParam
     * @return
     */
    @Override
    public List<PolicyEsVo> getPolicy(EsParam esParam) {

        String value = esParam.getValue();
        String pathIndex = esParam.getPathIndex();
        System.out.println("这里是新的：");
        List<PolicyEsVo> policyEsVoList = new ArrayList<>();
        switch (pathIndex) {
            case esIndex.policy:

                if (value == null || value.equals("")) {
                    List<Policy> policyList = policyMapper.selectList(null);
                    policyEsVoList = handlePolicy(policyList);
                } else {
                    QueryWrapper<Policy> wrapper = new QueryWrapper<>();
                    wrapper.like("title", value).or().like("place", value).or().like("content", value);
                    List<Policy> policyList = policyMapper.selectList(wrapper);
                    policyEsVoList = handlePolicy(policyList);
                }
                return policyEsVoList;


            case esIndex.commit:
                if (value == null || value.equals("")) {
                    List<Commited> commitedList = commitMapper.selectList(null);
                    policyEsVoList = handleCommit(commitedList);
                } else {
                    QueryWrapper<Commited> wrapper = new QueryWrapper<>();
                    wrapper.like("label", value).or().like("content", value);
                    List<Commited> commitedList = commitMapper.selectList(wrapper);
                    policyEsVoList = handleCommit(commitedList);
                }
                return policyEsVoList;


            case esIndex.video:
                if (value == null || value.equals("")) {
                    List<Video> videoList = videoMapper.selectList(null);
                    policyEsVoList = handleVideo(videoList);
                } else {
                    QueryWrapper<Video> wrapper = new QueryWrapper<>();
                    wrapper.like("name", value);
                    List<Video> videoList = videoMapper.selectList(wrapper);
                    policyEsVoList = handleVideo(videoList);
                }
                return policyEsVoList;
            default:
                break;
        }
        return null;
    }



}